package cn.hchaojie.javase.day01.exercise;

public class Gcd {
	public static void main(String[] args) {
		gcd3(6, 9);
		gcd3(6, 8);
		gcd3(4, 8);

		gcd3(3, 7);
	}
	
	public static void gcd(int x1, int x2) {
		while (true) {
			int divide = x1 % x2;
			if (divide == 0) {
				System.out.println(x2);
				break;
			} else {
				x1 = x2;
				x2 = divide;
			}
		}
	}
	
	public static void gcd2(int x1, int x2) {
		if (x1 < x2) {
			int temp = x2;
			x2 = x1;
			x1 = temp;
		}
		
		while (x1 != x2) {
			int subtract = x1 - x2;
			if (subtract > x2) {
				x1 = subtract;
			} else {
				x1 = x2;
				x2 = subtract;
			}
		}
		
		System.out.println(x1);
	}
	
	public static void gcd3(int x1, int x2) {
		int start = Math.min(x1, x2);
		for (; start >= 1; start--) {
			if (x1 % start == 0 && x2 % start == 0) {
				System.out.println(start);
				
				break;
			}
		}
	}
}
